'use strict'

//测试
/*var private_btn = document.getElementById("private");
private_btn.addEventListener('click',test_private);

var common_btn = document.getElementById("common");
common_btn.addEventListener('click',test_common);*/

/*var start_btn = document.getElementById("start");
start_btn.addEventListener('click',start);*/


/*window.room = prompt("Enter room name:");*/

var url = 'marcopolo';
var ws = new SockJS(url);
var stomp = Stomp.over(ws);
		
stomp.connect("","",connect_callback,error_callback);

stomp.onClosed = function(e){
	console.log(e);
	alert("与服务器断开连接");
}

stomp.onerror = function(e){
	console.log("发生错误");
}

function connect_callback (frame){
	console.log("连接成功");
	//订阅个人信息源
	stomp.subscribe("/user/queue/private",handlePrivateMessage);
	//订阅公共信息源
	stomp.subscribe("/queue/common",handleCommonMessage)
}

function error_callback (error){
	console.log("发生错误");
}

function handlePrivateMessage (frame){
	var msg = JSON.parse(frame.body);
	console.log("Received Private Message: "+msg.message);
	switch(msg.message){
	case "created":
		break;
	case "full":
		break;
	case "ipaddr":
		break;
	case "joined":
		break;
	case "log":
		break;
	}
}

function handleCommonMessage(frame){
	var msg = JSON.parse(frame.body);
	console.log("Received Common Message: "+msg.message);
}

//测试
/*function test_private(){
	stomp.send("/app/test-private",{},JSON.stringify({
		'message':'该信息发自客户端 private'
	}));
}

function test_common(){
	stomp.send("/app/test-common",{},JSON.stringify({
		'message':'该信息发自客户端 common'
	}));
}*/
